package com.etoak.controller;

import com.etoak.common.vo.AreaVO;
import com.etoak.common.vo.ResultVO;
import com.etoak.service.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class AreaController {

    @Autowired
    AreaService areaService;

    @PostMapping("/area")
    public ResultVO queryArea(){
        List<AreaVO> areaVOS = areaService.queryArea();
        //存放最顶层元素
        List<AreaVO> tops = new ArrayList<>();
        for (AreaVO m:areaVOS){
            if (m.getPid()==-1){
                tops.add(m);
            }
        }
        //给顶层的菜单设置子菜单
        for (AreaVO t:tops){
            areaService.setChildren(t,areaVOS);
        }

        return ResultVO.success(tops);
    }

    @PostMapping("/area/{pid}")
    public ResultVO queryByPid(@PathVariable int pid){
        List<AreaVO> areaVO = areaService.queryByPid(pid);
        return ResultVO.success(areaVO);
    }

}
